package com.unitd.modules.comm.exception;

import com.unitd.frame.springweb.controller.result.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @desc 异常信息处理器类,用于处理整个app遇到异常时的通用异常处理器
 * @filename GlobalExceptionHandler.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2018-01-30
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * @desc 处理异常信息
	 * @param e 异常
	 * @return 异常的json对象
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public AjaxResult exceptionHandler(Exception e) {
		AjaxResult resp = new AjaxResult();
		if (e instanceof ConfigServerException) {
			ConfigServerException e1 = (ConfigServerException) e;
			resp.setCode(e1.getCode());
			resp.setMsg(e1.getMessage());
		} else if (e instanceof org.springframework.web.HttpRequestMethodNotSupportedException) {
			resp.setCode(HttpStatus.METHOD_NOT_ALLOWED.value());
			resp.setMsg(e.getMessage());
		} else if (e instanceof org.springframework.web.HttpMediaTypeException) {
			resp.setCode(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value());
			resp.setMsg(e.getMessage());
		} else {
			resp.setCode(500);
			resp.setMsg("系统繁忙");
			logger.error("", e);
		}
		return resp;
	}
}